const fs = require('fs');
const path= require('path');
const rootPath = path.join(__dirname);
let pathInfo = [];
fs.stat(rootPath,(err, data) => {
    if(err) {
        console.log(err)
    }else{
        if(data.isFile()){
            pathInfo.push({
                path: rootPath,
                type: 'file'
            })
        }else if(data.isDirectory()){
            fs.readdir(rootPath,(err, data)=>{
                if(err){
                    console.log(err);
                }else{
                    if(data.length === 0) {
                        pathInfo.push({
                            path: rootPath,
                            type: 'directory'
                        })
                    }else{
                        let count = 0;
                       for(let item of data) {
                           let patStr = path.join(rootPath, item);
                           fs.stat(patStr,(err, stats)=>{
                               if(err) {
                                   console.log(err)
                               }else{
                                   if(stats.isFile()){
                                       pathInfo.push({
                                           path: patStr,
                                           type: 'file'
                                       })
                                   }else if(stats.isDirectory()){
                                       pathInfo.push({
                                           path: patStr,
                                           type: 'directory'
                                       })
                                   }
                                   count++;
                                   if(count === data.length){
                                       console.log(pathInfo);
                                   }
                               }
                           })
                       }
                    }
                }
            })
        }else{
            console.log(`${rootPath}既不是文件也不是目录`)
        }
    }
});

